<?php

namespace App\Models\News;
use Illuminate\Database\Eloquent\Model;

class NewsCategory extends Model{
    //设置表的类型和表名
    protected $table = 'news_category';
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $guarded = [];

    //返回所有类
    static public function cate($cate,$pid = 0,$html = '#',$level = 0){
        static $arr = array();
        static $parentname ='';
        foreach($cate as $v){

            if($v['pid'] == $pid ){
                $v['pid'] == 0 ?  $parentname = $v['defectsname'] : '';
                $v['parent'] = $parentname;
                $v['level'] = $level;
                $v['html'] = str_repeat($html,$level);
                $arr[] = $v;
                self::cate($cate,$v['id'], '#',$level+1);
            }
        }
        return $arr;
    }
    //返回类
    static public function allCate($cate,$pid = 0){
        $arr2 = array();
        foreach($cate as $v){
            if($v['pid'] == $pid ){
                $v['ddd'] = self::allCate($cate,$v['id']);
                $arr2[] = $v;
            }
        }
        return $arr2;
    }

    //传入父ID返回所有子类无限分类
    static public function parentCate($cate,$id = 9){
        $arr2 = array();
        foreach($cate as $v){
            if($v['pid'] == $id ){
                $v['ddd'] = self::parentCate($cate,$v['id']);
                $arr2[] = $v;
            }
        }
        return $arr2;
    }

    //传入子类ID返回所有父类 子类=》父类
    static public function childCate($cate,$id = 41){
        $arr2 = array();
        foreach($cate as $v){
            if($v['id'] == $id ){
                $v['ddd'] = self::childCate($cate,$v['pid']);
                $arr2[] = $v;
            }
        }
        return $arr2;
    }
}